﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using System.IO;
using ESRI.ArcGIS.Geometry;
using ZJGIS.EngineControl;
using ZJGIS.ADF.Node;
using ZJGIS.ADF.BaseClasses;
using ZJGIS.CoordCovert.model;
using DevComponents.DotNetBar;
using ZJGIS.EngineControl.OpenData;
using ZJGIS.Framework;
namespace ZJGIS.CoordCovert.form
{
    /// <summary>
    /// 将数据进行变形转换，使其可以在平台上进行叠加和查询
    /// </summary>
    public partial class FrmGeosocConvert : Office2007Form
    {
        private IApplication m_Application;
        private string folder;
        public FrmGeosocConvert(IApplication value)
        {
            InitializeComponent();
            m_Application = value;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                IList<IFeatureClass> fclasslist = new List<IFeatureClass>();
                OpenDataDialog addData = new OpenDataDialog();

                addData.OnOkClick = delegate
                {
                    List<IDataNode> datanodes = addData.GetAllDataNodes();
                    foreach (IDataNode datanode in datanodes)
                    {
                        if (datanode is FeatureClassNode)
                        {
                            fclasslist.Add(datanode.Open() as IFeatureClass);
                        }
                        else if (datanode is DatasetNode)
                        {
                            IEnumDataset datasetenum = datanode.Open().Subsets;
                            IDataset dataset = datasetenum.Next();
                            while (dataset != null)
                            {
                                fclasslist.Add(dataset as IFeatureClass);
                                dataset = datasetenum.Next();
                            }
                        }
                    }
                    foreach (IFeatureClass featureClass in fclasslist)
                    {

                        ListViewItem pListViewItem = new ListViewItem(((IDataset)featureClass).BrowseName);

                        pListViewItem.Tag = featureClass;
                        pListViewItem.Checked = true;
                        this.listView1.Items.Add(pListViewItem);

                    }
                    addData.Close();
                };
                addData.ShowDialog(this);
            }
            catch (Exception ex)
            {
                ControlUtility.ShowWarningMsgbox(ex.Message);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Items.Count == 0) return;
                if (this.listView1.SelectedItems.Count == 0) return;

                foreach (ListViewItem pListViewItem in this.listView1.SelectedItems)
                {
                    this.listView1.Items.Remove(pListViewItem);
                }
                //if (this.listView1.Items.Count == 0) this.listView1.Tag = null;
            }
            catch (Exception ex)
            {
                ControlUtility.ShowWarningMsgbox("移除失败：" + ex.Message);
            }
        }

        private void FrmAffine4Para_Load(object sender, EventArgs e)
        {
            txtMid.KeyPress += ControlUtility.txtDoubleBox_KeyPress;
            cboPrj.SelectedIndex = 0;
            cboZone.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 进度条相关

        private void setSubPgbMax(int max)
        {
            //this.Width = this.Width + progressBarSub.Width;
            progressBarSub.Maximum = max;
            progressBarSub.Minimum = 0;
            progressBarSub.Value = 0;
            Application.DoEvents();
        }

        //private void setSubPgbCurrent(int current)
        //{
        //    if (current <= progressBarSub.Maximum)
        //    {
        //        progressBarSub.Value = (int)current;

        //        double dRate = current * 100.0 / progressBarSub.Maximum;
        //        int iRate = (int)Math.Floor(dRate);
        //        //progressBarSub.TextVisible = true;
        //        //progressBarSub.Text = "分进度：" + current.ToString() + "/" + progressBarSub.Maximum.ToString() + "(" + iRate.ToString() + "%)";

        //    }
        //    else
        //    {
        //        progressBarSub.Value = (int)progressBarSub.Maximum;
        //        progressBarSub.TextVisible = false;
        //        //this.Width = this.Width - progressBarSub.Width;
        //    }
        //    progressBarSub.Update();
        //    if (progressBarSub.Maximum < 200)
        //        Application.DoEvents();
        //    else
        //        if (current % (progressBarSub.Maximum / 100) == 0) Application.DoEvents();
        //}

        private void setSubPgbCurrent(int current)
        {
            //if (current <= progressBarSub.Maximum)
            //{
            //    progressBarSub.Value = current;
            //}
            //else
            //{
            //    progressBarSub.Value = progressBarSub.Maximum;
            //}
            //Application.DoEvents();

            if (current <= progressBarSub.Maximum)
            {
                progressBarSub.Value = (int)current;

                double dRate = current * 100.0 / progressBarSub.Maximum;
                int iRate = (int)Math.Floor(dRate);
                progressBarSub.TextVisible = true;
                progressBarSub.Text = "分进度：" + current.ToString() + "/" + progressBarSub.Maximum.ToString() + "(" + iRate.ToString() + "%)";

                //dataGridViewX1.UseWaitCursor = true;
            }
            else
            {
                progressBarSub.Value = (int)progressBarSub.Maximum;
                progressBarSub.TextVisible = false;
                //dataGridViewX1.UseWaitCursor = false;
            }
            progressBarSub.Update();
            if (progressBarSub.Maximum < 200)
                Application.DoEvents();
            else
                if (current % (progressBarSub.Maximum / 100) == 0) Application.DoEvents();
        }

        private void setAllPgbMax(int max)
        {
            //this.Width = this.Width + progressBarAll.Width;
            progressBarAll.Maximum = max;
            progressBarAll.Minimum = 0;
            progressBarAll.Value = 0;
            Application.DoEvents();
        }

        //private void setAllPgbCurrent(int current)
        //{
        //    if (current <= progressBarAll.Maximum)
        //    {
        //        progressBarAll.Value = (int)current;

        //        double dRate = current * 100.0 / progressBarAll.Maximum;
        //        int iRate = (int)Math.Floor(dRate);
        //        //progressBarAll.TextVisible = true;
        //        //progressBarAll.Text = "总进度：" + current.ToString() + "/" + progressBarAll.Maximum.ToString() + "(" + iRate.ToString() + "%)";
        //    }
        //    else
        //    {
        //        progressBarAll.Value = (int)progressBarAll.Maximum;
        //        progressBarAll.TextVisible = false;
        //        //this.Width = this.Width - progressBarAll.Width;
        //    }
        //    progressBarAll.Update();
        //    if (progressBarAll.Maximum < 200)
        //        Application.DoEvents();
        //    else
        //        if (current % (progressBarAll.Maximum / 100) == 0) Application.DoEvents();
        //}

        private void setAllPgbCurrent(int current)
        {
            //if (current <= progressBarAll.Maximum)
            //{
            //    progressBarAll.Value = current;
            //}
            //else
            //{
            //    progressBarAll.Value = progressBarAll.Maximum;
            //}
            //Application.DoEvents();

            if (current <= progressBarAll.Maximum)
            {
                progressBarAll.Value = (int)current;

                double dRate = current * 100.0 / progressBarAll.Maximum;
                int iRate = (int)Math.Floor(dRate);
                progressBarAll.TextVisible = true;
                progressBarAll.Text = "总进度：" + current.ToString() + "/" + progressBarAll.Maximum.ToString() + "(" + iRate.ToString() + "%)";

                //dataGridViewX1.UseWaitCursor = true;
            }
            else
            {
                progressBarAll.Value = (int)progressBarAll.Maximum;
                progressBarAll.TextVisible = false;
                //dataGridViewX1.UseWaitCursor = false;
            }
            progressBarAll.Update();
            if (progressBarAll.Maximum < 200)
                Application.DoEvents();
            else
                if (current % (progressBarAll.Maximum / 100) == 0) Application.DoEvents();
        }

        #endregion

        private void btnConvert_Click(object sender, EventArgs e)
        {
            List<IFeatureClass> pFeatClses = new List<IFeatureClass>();

            foreach (ListViewItem pListViewItem in this.listView1.Items)
            {
                if (pListViewItem.Checked && pListViewItem.Tag != null && pListViewItem.Tag is IFeatureClass)
                {
                    pFeatClses.Add(pListViewItem.Tag as IFeatureClass);
                }
            }

            if (pFeatClses.Count == 0)
            {
                ControlUtility.ShowWarningMsgbox("请设置需要转换的数据！");
                return;
            }

            int prj = cboPrj.SelectedIndex - 1;
            int zone = cboZone.SelectedIndex;
            double mid = Double.Parse(txtMid.Text);
            string url = txtUrl.Text;

            GeosocConvert geosocConvert = new GeosocConvert();
            geosocConvert.Prj = prj;
            geosocConvert.Zone = zone;
            geosocConvert.Mid = mid;
            geosocConvert.Url = url;
            geosocConvert.FeatClses = pFeatClses;
            geosocConvert.WriteLog = logPanel1.Log;
            geosocConvert.ErrLog = logPanel1.ErrLog;
            geosocConvert.DoneLog = logPanel1.DoneLog;
            geosocConvert.SetProcessBarMax = setAllPgbMax;
            geosocConvert.SetProcessBarCurrent = setAllPgbCurrent;
            geosocConvert.SetSubProcessBarMax = setSubPgbMax;
            geosocConvert.SetSubProcessBarCurrent = setSubPgbCurrent;

            btnConvert.Enabled = false;
            btnCancel.Enabled = false;

            if (geosocConvert.Operate())
            {
                ControlUtility.ShowInfomationMsgbox("转换完毕！");
            }
            else
            {
                ControlUtility.ShowErrorMsgbox("转换失败，具体信息请参看日志！");
            }

            btnConvert.Enabled = true;
            btnCancel.Enabled = true;
            progressBarAll.Value = 0;
            progressBarAll.Text = "";
            progressBarSub.Value = 0;
            progressBarSub.Text = "";
        }
    }
}
